#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import * 
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "mpc current"

lcd.begin(16,2)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output


start_index = 0
prev_content = ''

while 1:
	current = run_cmd(cmd)[:-1]
        print "Current = " + "[" + current + "]"
        if current != prev_content and prev_content != '':
          start_index = 0
        display_content = '                ' + current + '                '
	lcd.setCursor(0,0)
	lcd.message(datetime.now().strftime('%b %d  %H:%M\n'))
	lcd.setCursor(0,1)
	lcd.message('')
        lcd.message(display_content[start_index:start_index+16])
        print "[" + display_content[start_index:start_index+16] + "]"
        start_index = start_index + 1
        if start_index > len(current) + 16:
          start_index = 0
        prev_content = current
	sleep(0.5)
